package io.mya.modules.sys.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.mya.common.annotation.SysLog;
import io.mya.common.utils.PageUtils;
import io.mya.common.utils.Query;
import io.mya.common.utils.R;
import io.mya.common.validator.ValidatorUtils;
import io.mya.modules.sys.entity.CandyUserEntity;
import io.mya.modules.sys.service.CandyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/candyusermanager")
public class CandyUserManagerController {

    @Autowired
    private CandyUserService candyUserService;
    private Date validity;

    /**
     * 添加   
     *
     */    
    @RequestMapping("/save")
    @RequiresPermissions("sys:candyuser:save")
    @ResponseBody
    public R save(@RequestBody CandyUserEntity product){
        product.setStatus(1);
        candyUserService.save(product);
        System.err.println(R.ok());
        return R.ok();
    }
    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("sys:candyusercandy:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        Page<CandyUserEntity> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<CandyUserEntity> sysLogList = candyUserService.queryList(query);
        //从page里面拿到表里面的总数
        int total= new Long(page.getTotal()).intValue();
        PageUtils pageUtil = new PageUtils(sysLogList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 删除
     *
     *@param id:id(批量删除)
     */
    @ResponseBody
    @SysLog("删除优惠券")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] id){
        candyUserService.delete(id);
        return R.ok();
    }
//
    /**
     * 根据id查询信息(修改回显)
     *
     * @param   id:客户id
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        CandyUserEntity candyusercandy= candyUserService.queryObject(id);
        return R.ok().put("product", candyusercandy);
    }
    /**
     * 修改客户信息
     *    
     */
    @SysLog("修改客户信息")
    @RequestMapping("/update") 
    @ResponseBody
    public R update(@RequestBody CandyUserEntity product){
        ValidatorUtils.validateEntity(product);
        candyUserService.update(product);
        return R.ok();
    }

    @ResponseBody
    @RequestMapping("/updatea/{id}")
    public R delete(@PathVariable("id") Long id){
        CandyUserEntity candyusercand=new CandyUserEntity();
        CandyUserEntity queryObject = candyUserService.queryObject(id);
        if(queryObject.getType()==1 || queryObject.getType()==null){
            candyusercand.setId(queryObject.getId());
            candyusercand.setType(2);
            candyUserService.update(candyusercand);
        }else if (queryObject.getType()==2) {
            candyusercand.setId(queryObject.getId());
            candyusercand.setType(1);
            candyUserService.update(candyusercand);
        }
        return R.ok();
    }
}
